package view.game
{
	import flash3.framework.Singleton;
	public class GameLogic
	{
		//排序类型
		public static const ST_ORDER:uint					=0;									//大小排序
		public static const ST_COUNT:uint					=1;									//数目排序
		
		//////////////////////////////////////////////////////////////////////////
		//数目定义
		
		public static const MAX_COUNT:uint					=20									//最大数目
		public static const FULL_COUNT:uint				=	54									//全牌数目
		public static const BACK_COUNT	:uint				=3									//底牌数目
		public static const NORMAL_COUNT:uint				=17									//常规数目
		
		//////////////////////////////////////////////////////////////////////////
		
		//数值掩码
		public static const MASK_COLOR:uint				=	0xF0								//花色掩码
		public static const MASK_VALUE:uint				=	0x0F								//数值掩码
		
		//扑克类型
		public static const CT_ERROR:uint					=	0									//错误类型
		public static const CT_SINGLE:uint					=1									//单牌类型
		public static const CT_DOUBLE:uint					=2									//对牌类型
		public static const CT_THREE:uint					=	3									//三条类型
		public static const CT_SINGLE_LINE:uint			=	4									//单连类型
		public static const CT_DOUBLE_LINE:uint			=	5									//对连类型
		public static const CT_THREE_LINE:uint				=6									//三连类型
		public static const CT_THREE_LINE_TAKE_ONE:uint	=	7									//三带一单
		public static const CT_THREE_LINE_TAKE_TWO:uint	=	8									//三带一对
		public static const CT_FOUR_LINE_TAKE_ONE:uint		=9									//四带两单
		public static const CT_FOUR_LINE_TAKE_TWO:uint		=10									//四带两对
		public static const CT_BOMB_CARD:uint				=	11									//炸弹类型
		public static const CT_MISSILE_CARD:uint			=	12									//火箭类型
		
		public static const m_cbCardData:Array = [
			0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,	//方块 A - K
			0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D,			//梅花 A - K
			0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2A,0x2B,0x2C,0x2D,			//红桃 A - K
			0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,0x3C,0x3D,			//黑桃 A - K
			0x4E,0x4F,
		];//扑克数据
		
		public function GameLogic()
		{
		}
		
		static public function get i():GameLogic
		{
			return Singleton.getInstanceOrCreate(GameLogic) as GameLogic;
		}
		
		//类型函数
		//获取类型
		public function GetCardType(cbCardData:Array):uint
		{
			var i:uint = 0;
			var cbCardValue:uint;
			var cbFirstLogicValue:uint;
			var cbCardCount:uint = cbCardData ? cbCardData.length : 0;
			//简单牌型
			switch (cbCardCount)
			{
				case 0:	//空牌
				{
					return CT_ERROR;
				}
				case 1: //单牌
				{
					return CT_SINGLE;
				}
				case 2:	//对牌火箭
				{
					//牌型判断
					if ((cbCardData[0]==0x4F)&&(cbCardData[1]==0x4E)) return CT_MISSILE_CARD;
					if (GetCardLogicValue(cbCardData[0])==GetCardLogicValue(cbCardData[1])) return CT_DOUBLE;
					
					return CT_ERROR;
				}
			}
			
			//分析扑克
			var analyseResult:AnalyseResult = new AnalyseResult();
			AnalysebCardData(cbCardData,analyseResult);
			
			//四牌判断
			if (analyseResult.cbFourCount>0)
			{
				//牌型判断
				if ((analyseResult.cbFourCount==1)&&(cbCardCount==4)) return CT_BOMB_CARD;
				//	if ((analyseResult.cbFourCount==1)&&(analyseResult.cbSignedCount==2)&&(cbCardCount==6)) return CT_FOUR_LINE_TAKE_ONE;
				if ((analyseResult.cbFourCount==1)&&(analyseResult.cbSignedCount==2)&&(cbCardCount==6)) return CT_FOUR_LINE_TAKE_ONE;
				if ((analyseResult.cbFourCount==1)&&(analyseResult.cbDoubleCount==2)&&(cbCardCount==8)) return CT_FOUR_LINE_TAKE_TWO;
				
				return CT_ERROR;
			}
			
			//三牌判断
			if (analyseResult.cbThreeCount>0)
			{
				//三条类型
				if(analyseResult.cbThreeCount==1 && cbCardCount==3) return CT_THREE ;
				
				//连牌判断
				if (analyseResult.cbThreeCount>1)
				{
					//变量定义
					cbCardValue = analyseResult.cbThreeCardData[0][0];
					cbFirstLogicValue = GetCardLogicValue(cbCardValue);
					
					//错误过虑
					if (cbFirstLogicValue>=15) return CT_ERROR;
					
					//连牌判断
					for (i=1;i<analyseResult.cbThreeCount;i++)
					{
						cbCardValue = analyseResult.cbThreeCardData[i][0];
						if (cbFirstLogicValue+i != GetCardLogicValue(cbCardValue)) return CT_ERROR;
					}
				}
				
				//牌形判断
				if (analyseResult.cbThreeCount*3==cbCardCount) return CT_THREE_LINE;
				if (analyseResult.cbThreeCount*4==cbCardCount) return CT_THREE_LINE_TAKE_ONE;
				if ((analyseResult.cbThreeCount*5==cbCardCount)&&(analyseResult.cbDoubleCount==analyseResult.cbThreeCount)) return CT_THREE_LINE_TAKE_TWO;
				
				return CT_ERROR;
			}
			
			//两张类型
			if (analyseResult.cbDoubleCount>=3)
			{
				//变量定义
				cbCardValue=analyseResult.cbDoubleCardData[0][0];
				cbFirstLogicValue=GetCardLogicValue(cbCardValue);
				
				//错误过虑
				if (cbFirstLogicValue>=15) return CT_ERROR;
				
				//连牌判断
				for (i=1;i<analyseResult.cbDoubleCount;i++)
				{
					cbCardValue=analyseResult.cbDoubleCardData[i][0];
					if (cbFirstLogicValue+i != GetCardLogicValue(cbCardValue)) return CT_ERROR;
				}
				
				//二连判断
				if ((analyseResult.cbDoubleCount*2)==cbCardCount) return CT_DOUBLE_LINE;
				
				return CT_ERROR;
			}
			
			//单张判断
			if ((analyseResult.cbSignedCount>=5)&&(analyseResult.cbSignedCount==cbCardCount))
			{
				//变量定义
				cbCardValue = analyseResult.cbSignedCardData[0];
				cbFirstLogicValue = GetCardLogicValue(cbCardValue);
				
				//错误过虑
				if (cbFirstLogicValue>=15) return CT_ERROR;
				
				//连牌判断
				for (i=1;i<analyseResult.cbSignedCount;i++)
				{
					cbCardValue=analyseResult.cbSignedCardData[i];
					if (cbFirstLogicValue+i != GetCardLogicValue(cbCardValue)) return CT_ERROR;
				}
				
				return CT_SINGLE_LINE;
			}
			
			return CT_ERROR;
		}
		//获取数值
		public function GetCardValue(cbCardData:uint):uint 
		{ 
			return cbCardData&MASK_VALUE; 
		}
		//获取花色
		public function GetCardColor(cbCardData:uint):uint
		{ 
			return cbCardData&MASK_COLOR; 
		}
		
		//控制函数
		//混乱扑克
		public function RandCardList():Array
		{
			var cbCardBuffer:Array = m_cbCardData.slice();
			function sortF(a:*,b:*):int 
			{
				return int(Math.random()*52);//乱序用，增加了返回值的范围，便于更加彻底的乱序
			}
			cbCardBuffer.sort(sortF);
			return cbCardBuffer;
		}
		//排列扑克
		public function SortCardList(cbCardData:Array, cbSortType:uint):void
		{
			var cbCardCount:uint = cbCardData.length;
			//数目过虑
			if (cbCardCount==0) return;
			
			//转换数值
			var i:uint = 0; var j:uint = 0;
			var cbSortValue:Array = [];
			for (i=0;i<cbCardCount;i++) 
				cbSortValue[i]=GetCardLogicValue(cbCardData[i]);	
			
			//排序操作
			var bSorted:Boolean=true;
			var cbThreeCount:uint;
			var cbLast:uint=cbCardCount-1;
			do
			{
				bSorted=true;
				for (i=0;i<cbLast;i++)
				{
					if ((cbSortValue[i]<cbSortValue[i+1])||
						((cbSortValue[i]==cbSortValue[i+1])&&(cbCardData[i]<cbCardData[i+1])))
					{
						//交换位置
						cbThreeCount=cbCardData[i];
						cbCardData[i]=cbCardData[i+1];
						cbCardData[i+1]=cbThreeCount;
						cbThreeCount=cbSortValue[i];
						cbSortValue[i]=cbSortValue[i+1];
						cbSortValue[i+1]=cbThreeCount;
						bSorted=false;
					}	
				}
				cbLast--;
			} while(bSorted==false);
			
			//数目排序
			if (cbSortType==ST_COUNT)
			{
				//分析扑克
				var cbIndex:uint=0;
				var analyseResult:AnalyseResult = new AnalyseResult();
				AnalysebCardData(cbCardData,analyseResult);
				cbCardData.length = 0;
				var tempCardData:Array = null;
				tempCardData = analyseResult.cbFourCardData;
				for(i=0;i<tempCardData.length;i++)
				{
					for(j = 0; j < tempCardData[i].length; j++)
						cbCardData.push(tempCardData[i][j]);
				}
				tempCardData = analyseResult.cbThreeCardData;
				for(i=0;i<tempCardData.length;i++)
				{
					for(j = 0; j < tempCardData[i].length; j++)
						cbCardData.push(tempCardData[i][j]);
				}
				tempCardData = analyseResult.cbDoubleCardData;
				for(i=0;i<tempCardData.length;i++)
				{
					for(j = 0; j < tempCardData[i].length; j++)
						cbCardData.push(tempCardData[i][j]);
				}
				tempCardData = analyseResult.cbSignedCardData;
				for(i=0;i<tempCardData.length;i++)
				{
					cbCardData.push(tempCardData[i]);
				}
			}
		}
		//删除扑克
		public function RemoveCard(cbRemoveCard:Array, cbCardData:Array):Boolean
		{
			var cbRemoveCount:uint = cbRemoveCard.length;
			var cbCardCount:uint = cbCardData.length;
			//检验数据
			if(cbRemoveCount<=cbCardCount) return false;
			
			//定义变量
			var cbDeleteCount:uint=0;
			var cbTempCardData:Array = [MAX_COUNT];
			if (cbCardCount>cbTempCardData.length) return false;
			cbTempCardData = cbCardData.slice();
			
			//置零扑克
			for (var i:uint=0;i<cbRemoveCount;i++)
			{
				for (var j:uint=0;j<cbCardCount;j++)
				{
					if (cbRemoveCard[i]==cbTempCardData[j])
					{
						cbDeleteCount++;
						cbTempCardData[j]=0;
						break;
					}
				}
			}
			if (cbDeleteCount!=cbRemoveCount) return false;
			
			//清理扑克
			var cbCardPos:uint=0;
			for (i=0;i<cbCardCount;i++)
			{
				if (cbTempCardData[i]!=0) 
					cbCardData[cbCardPos++]=cbTempCardData[i];
			}
			
			return true;
		}
		
		//逻辑函数
		public function IsValidCard(cbCardData:uint):Boolean
		{
			//获取属性
			var cbCardColor:uint=GetCardColor(cbCardData);
			var cbCardValue:uint=GetCardValue(cbCardData);
			
			//有效判断
			if ((cbCardData==0x4E)||(cbCardData==0x4F)) return true;
			if ((cbCardColor<=0x30)&&(cbCardValue>=0x01)&&(cbCardValue<=0x0D)) return true;
			
			return false;
		}
		//逻辑数值
		public function GetCardLogicValue(cbCardData:uint):uint
		{
			//扑克属性
			var cbCardColor:uint=GetCardColor(cbCardData);
			var cbCardValue:uint=GetCardValue(cbCardData);
			
			//转换数值
			if (cbCardColor==0x40) return cbCardValue+2;
			return (cbCardValue<=2)?(cbCardValue+13):cbCardValue;
		}

		//对比扑克
		public function CompareCard(cbFirstCard:Array, cbNextCard:Array):Boolean
		{
			this.SortCardList(cbFirstCard, ST_ORDER);
			this.SortCardList(cbNextCard, ST_ORDER);
			if(!cbNextCard || cbNextCard.length==0 || GetCardType(cbNextCard)==CT_ERROR) return false;
			if(!cbFirstCard || cbFirstCard.length==0) return true;
			
			var cbNextCount:uint= cbNextCard.length;
			var cbFirstCount:uint= cbFirstCard.length;
			//获取类型
			var cbNextType:uint=GetCardType(cbNextCard);
			var cbFirstType:uint=GetCardType(cbFirstCard);
			
			//类型判断
			if (cbNextType==CT_ERROR) return false;
			if (cbNextType==CT_MISSILE_CARD) return true;
			
			//炸弹判断
			if ((cbFirstType!=CT_BOMB_CARD)&&(cbNextType==CT_BOMB_CARD)) return true;
			if ((cbFirstType==CT_BOMB_CARD)&&(cbNextType!=CT_BOMB_CARD)) return false;
			
			//规则判断
			if ((cbFirstType!=cbNextType)||(cbFirstCount!=cbNextCount)) return false;
			
			//开始对比
			switch (cbNextType)
			{
				case CT_SINGLE:
					//获取数值
					var cbNextLogicValue:uint=GetCardLogicValue(cbNextCard[0]);
					var cbFirstLogicValue:uint=GetCardLogicValue(cbFirstCard[0]);
					
					//对比扑克
					return cbNextLogicValue>cbFirstLogicValue;
				case CT_DOUBLE:
				case CT_THREE:
				case CT_SINGLE_LINE:
				case CT_DOUBLE_LINE:
				case CT_THREE_LINE:
				case CT_BOMB_CARD:
				{
					//获取数值
					var cbNextLogicValue:uint=GetCardLogicValue(cbNextCard[0]);
					var cbFirstLogicValue:uint=GetCardLogicValue(cbFirstCard[0]);
					
					//对比扑克
					return cbNextLogicValue>cbFirstLogicValue;
				}
				case CT_THREE_LINE_TAKE_ONE:
				case CT_THREE_LINE_TAKE_TWO:
				{
					//分析扑克
					var NextResult:AnalyseResult = new AnalyseResult() ;
					var FirstResult:AnalyseResult = new AnalyseResult() ;
					AnalysebCardData(cbNextCard,NextResult);
					AnalysebCardData(cbFirstCard,FirstResult);
					
					//获取数值
					var cbNextLogicValue:uint=GetCardLogicValue(NextResult.cbThreeCardData[0][0]);
					var cbFirstLogicValue:uint=GetCardLogicValue(FirstResult.cbThreeCardData[0][0]);
					
					//对比扑克
					return cbNextLogicValue>cbFirstLogicValue;
				}
				case CT_FOUR_LINE_TAKE_ONE:
				case CT_FOUR_LINE_TAKE_TWO:
				{
					//分析扑克
					var NextResult:AnalyseResult = new AnalyseResult() ;
					var FirstResult:AnalyseResult = new AnalyseResult() ;
					AnalysebCardData(cbNextCard,NextResult);
					AnalysebCardData(cbFirstCard,FirstResult);
					
					//获取数值
					var cbNextLogicValue:uint=GetCardLogicValue(NextResult.cbFourCardData[0][0]);
					var cbFirstLogicValue:uint=GetCardLogicValue(FirstResult.cbFourCardData[0][0]);
					
					//对比扑克
					return cbNextLogicValue>cbFirstLogicValue;
				}
			}
			
			return false;
		}
		
		//内部函数
		//分析扑克
		public function AnalysebCardData(cbCardData:Array, analyseResult:AnalyseResult):void
		{
			//扑克分析
			analyseResult.reset();
			var cbIndex:uint = 0;
			var cbCardCount:uint = cbCardData.length;
			var i:int = 0;
			var j:int = 0;
			var k:int = 0;
			var tempCardData:Array = [];
			this.SortCardList(cbCardData, ST_ORDER);
			for (i=cbCardCount-1;i>=0;i--)
			{
				//变量定义
				var cbSameCount:uint=1;
				var cbCardValueTemp:uint=0;
				var cbLogicValue:uint=GetCardLogicValue(cbCardData[i]);
				
				//搜索同牌
				for (j=i-1;j>=0;j--)
				{
					var cbValue:uint = GetCardLogicValue(cbCardData[j]);
					if(cbLogicValue == 16 && cbValue == 17)
					{
						analyseResult.cbMissileCount = 1;
						analyseResult.cbMissileCardData.push([cbCardData[i], cbCardData[j]]);
					}
					//获取扑克
					if (cbValue!=cbLogicValue) break;
					
					//设置变量
					cbSameCount++;
				}
				
				//设置结果
				switch (cbSameCount)
				{
					case 1:		//单张
					{
						cbIndex=analyseResult.cbSignedCount++;
						analyseResult.cbSignedCardData[cbIndex]=cbCardData[i];
						break;
					}
					case 2:		//两张
					{
						cbIndex=analyseResult.cbDoubleCount++;
						analyseResult.cbDoubleCardData[cbIndex]=[cbCardData[i],cbCardData[i-1]];
						break;
					}
					case 3:		//三张
					{
						cbIndex=analyseResult.cbThreeCount++;
						analyseResult.cbThreeCardData[cbIndex]=[cbCardData[i],cbCardData[i-1],cbCardData[i-2]];
						break;
					}
					case 4:		//四张
					{
						cbIndex=analyseResult.cbFourCount++;
						analyseResult.cbFourCardData[cbIndex]=[cbCardData[i],cbCardData[i-1],cbCardData[i-2],cbCardData[i-3]];
						break;
					}
				}
				
				//设置索引
				i-=cbSameCount-1;
			}
			
			var offset:uint = 0;
			var cbHandLogicValue:uint = 0;
			var result:Array = [];
			var tempCardData1:Array = [];
			var tempCardData2:Array = [];
			//飞机
			if(cbCardData.length > 6 && analyseResult.cbThreeCount+analyseResult.cbFourCount >= 2)
			{
				tempCardData = analyseResult.cbThreeCardData;
				for(i = 0; i < tempCardData.length; i++)
				{
					if(GetCardLogicValue(tempCardData[i][0]) != 15)
					{
						for(j = 0; j < 3; j++)
							tempCardData1.push(tempCardData[i][j]);
					}
				}
				tempCardData = analyseResult.cbFourCardData;
				for(i = 0; i < tempCardData.length; i++)
				{
					if(GetCardLogicValue(tempCardData[i][0]) != 15)
					{
						for(j = 0; j < 3; j++)
							tempCardData1.push(tempCardData[i][j]);
					}
				}
				SortCardList(tempCardData1, ST_ORDER);
				for(i = tempCardData1.length / 3 - 1; i >= 0; i--)
					tempCardData2.push([tempCardData1[i*3], tempCardData1[i*3+1], tempCardData1[i*3+2]]);
				var aircraftCardData:Array = analyseResult.cbAircraftCardData;
				for(i = 0; i < tempCardData2.length; i++)
				{
					cbLogicValue = GetCardLogicValue(tempCardData2[i][0]);
					result.length = 0;
					result.push(tempCardData2[i][0], tempCardData2[i][1], tempCardData2[i][2]);
					for(j = i+1; j < tempCardData2.length; j++)
					{
						cbHandLogicValue = GetCardLogicValue(tempCardData2[j][0]);
						offset = cbHandLogicValue - GetCardLogicValue(result[result.length-1]);
						if(offset == 1)
						{
							result.push(tempCardData2[j][0], tempCardData2[j][1], tempCardData2[j][2]);
							if(result.length >= 6)
							{
								aircraftCardData.push(result.slice());
								analyseResult.cbAircraftCount++;
							}
						}
						else if(offset != 0)
						{
							break;
						}
					}
				}
			}
			
			//单连
			if (cbCardCount < 5)
			{
				analyseResult.cbSignedLineCount = 0;
			}
			else
			{
				var logicValue1:uint = 0;
				var logicValue2:uint = 0;
				var logicValue3:uint = 0;
				var isNext:Boolean = false;
				var signedLineData:Array = analyseResult.cbSignedLineCardData;
				for(i = cbCardCount-1; i >= 0; i--)
				{
					logicValue1 = GetCardLogicValue(cbCardData[i]);
					if(logicValue1 > 10) break;
					result.length = 0;
					result.push(cbCardData[i]);
					for(k = i-1; k >= 0; k--)
					{
						logicValue2 = GetCardLogicValue(cbCardData[k]);
						if(logicValue1 == logicValue2 || logicValue2 > 14) break;
						if(logicValue2 - GetCardLogicValue(result[result.length-1]) == 1)
						{
							result.push(cbCardData[k]);
							if(result.length >= 5)
							{
								signedLineData.push(result.slice());
								analyseResult.cbSignedLineCount++;
							}
						}
					}
				}
			}
			
			//对连
			if (cbCardCount < 6)
			{
				analyseResult.cbDoubleLineCount = 0;
			}
			else
			{
				tempCardData1.length = 0;
				tempCardData2.length = 0;
				tempCardData = analyseResult.cbDoubleCardData;
				for(i = 0; i < tempCardData.length; i++)
				{
					if(GetCardLogicValue(tempCardData[i][0]) != 15)
					{
						for(j = 0; j < 2; j++)
							tempCardData1.push(tempCardData[i][j]);
					}
				}
				tempCardData = analyseResult.cbThreeCardData;
				for(i = 0; i < tempCardData.length; i++)
				{
					if(GetCardLogicValue(tempCardData[i][0]) != 15)
					{
						for(j = 0; j < 2; j++)
							tempCardData1.push(tempCardData[i][j]);
					}
				}
				tempCardData = analyseResult.cbFourCardData;
				for(i = 0; i < tempCardData.length; i++)
				{
					if(GetCardLogicValue(tempCardData[i][0]) != 15)
					{
						for(j = 0; j < 2; j++)
							tempCardData1.push(tempCardData[i][j]);
					}
				}
				SortCardList(tempCardData1, ST_ORDER);
				for(i = tempCardData1.length / 2 - 1; i >= 0; i--)
					tempCardData2.push([tempCardData1[i*2], tempCardData1[i*2+1]]);
				var doubleLineData:Array = analyseResult.cbDoubleLineCardData;
				for(i = 0; i < tempCardData2.length; i++)
				{
					cbLogicValue = GetCardLogicValue(tempCardData2[i][0]);
					if(cbLogicValue != 15) //!=2
					{
						result.length = 0;
						result.push(tempCardData2[i][0], tempCardData2[i][1]);
						for(j = i+1; j < tempCardData2.length; j++)
						{
							cbHandLogicValue = GetCardLogicValue(tempCardData2[j][0]);
							offset = cbHandLogicValue - GetCardLogicValue(result[result.length-1]);
							if(offset == 1)
							{
								result.push(tempCardData2[j][0], tempCardData2[j][1]);
								if(result.length >= 6)
								{
									doubleLineData.push(result.slice());
									analyseResult.cbDoubleLineCount++;
								}
							}
							else if(offset != 0)
							{
								break;
							}
						}
					}
				}
			}
			
		}
		public function serchCardData(cbHandCardData:Array, cbTurnCardData:Array, resultCardData:Array, analyResult:AnalyseResult):void
		{
			resultCardData.length = 0;
			var handCarddData:Array = cbHandCardData.slice();
			var turnCarddData:Array = cbTurnCardData.slice();
			SortCardList(handCarddData, ST_ORDER);
			SortCardList(turnCarddData, ST_ORDER);
			var turnCardType:uint = GetCardType(cbTurnCardData);
			var handleCardCount:uint = cbHandCardData.length;
			var cardLogicValue1:uint; var cardLogicValue2:uint;
			var cardDataValue:int = -1;
			var i:int = 0; var j:int = 0; var k:int = 0
			var tempCardData:Array = null;
			var tempCardData2:Array = null;
			var cardDataValues:Array = [];
			var tempCardData1:Array;
			var cardData:Array = [];
			var isPush:Boolean = false;
			switch(turnCardType)
			{
				case CT_ERROR:
					cardLogicValue1 = GetCardLogicValue(handCarddData[handleCardCount-1]);
					resultCardData.push([handCarddData[handleCardCount-1]]);
					var cbSameCount:uint=1;
					for(i = handleCardCount-2; i >= 0; i--)
					{
						if (GetCardLogicValue(handCarddData[i]) == cardLogicValue1) 
							resultCardData[0].push(handCarddData[i]);
						else break;
					}
					break;
				case CT_SINGLE:
					cardLogicValue1 = GetCardLogicValue(turnCarddData[0]);
					tempCardData = analyResult.cbSignedCardData;
					for(i = 0; i < tempCardData.length; i++)
					{
						if(GetCardLogicValue(tempCardData[i]) > cardLogicValue1)
							resultCardData.push([tempCardData[i]]);
					}
					tempCardData = analyResult.cbDoubleCardData;
					for(i = 0; i < tempCardData.length; i++)
					{
						if(GetCardLogicValue(tempCardData[i][0]) > cardLogicValue1)
							resultCardData.push([tempCardData[i][0]]);
					}
					tempCardData = analyResult.cbThreeCardData;
					for(i = 0; i < tempCardData.length; i++)
					{
						if(GetCardLogicValue(tempCardData[i][0]) > cardLogicValue1)
							resultCardData.push([tempCardData[i][0]]);
					}
					tempCardData = analyResult.cbFourCardData;
					for(i = 0; i < tempCardData.length; i++)
					{
						if(GetCardLogicValue(tempCardData[i][0]) > cardLogicValue1)
							resultCardData.push([tempCardData[i][0]]);
					}
					tempCardData = analyResult.cbFourCardData;
					for(i = 0; i < tempCardData.length; i++)
						resultCardData.push(tempCardData[i].slice());
					tempCardData = analyResult.cbMissileCardData;
					for(i = 0; i < tempCardData.length; i++)
						resultCardData.push(tempCardData[i].slice());
					break;
				case CT_DOUBLE:
					cardLogicValue1 = GetCardLogicValue(turnCarddData[0]);
					tempCardData = analyResult.cbDoubleCardData;
					for(i = 0; i < tempCardData.length; i++)
					{
						if(GetCardLogicValue(tempCardData[i][0]) > cardLogicValue1)
							resultCardData.push(tempCardData[i].slice());
					}
					tempCardData = analyResult.cbThreeCardData;
					for(i = 0; i < tempCardData.length; i++)
					{
						if(GetCardLogicValue(tempCardData[i][0]) > cardLogicValue1)
							resultCardData.push(tempCardData[i].slice(0, 2));
					}
					tempCardData = analyResult.cbFourCardData;
					for(i = 0; i < tempCardData.length; i++)
					{
						if(GetCardLogicValue(tempCardData[i][0]) > cardLogicValue1)
							resultCardData.push(tempCardData[i].slice(0, 2));
					}
					tempCardData = analyResult.cbFourCardData;
					for(i = 0; i < tempCardData.length; i++)
						resultCardData.push(tempCardData[i].slice());
					tempCardData = analyResult.cbMissileCardData;
					for(i = 0; i < tempCardData.length; i++)
						resultCardData.push(tempCardData[i].slice());
					break;
				case CT_THREE:
					cardLogicValue1 = GetCardLogicValue(turnCarddData[0]);
					tempCardData = analyResult.cbThreeCardData;
					for(i = 0; i < tempCardData.length; i++)
					{
						if(GetCardLogicValue(tempCardData[i][0]) > cardLogicValue1)
							resultCardData.push(tempCardData[i].slice(0, 3));
					}
					tempCardData = analyResult.cbFourCardData;
					for(i = 0; i < tempCardData.length; i++)
					{
						if(GetCardLogicValue(tempCardData[i][0]) > cardLogicValue1)
							resultCardData.push(tempCardData[i].slice(0, 3));
					}
					tempCardData = analyResult.cbFourCardData;
					for(i = 0; i < tempCardData.length; i++)
					{
						if(GetCardLogicValue(tempCardData[i][0]) > cardLogicValue1)
							resultCardData.push(tempCardData[i].slice(0, 3));
					}
					tempCardData = analyResult.cbFourCardData;
					for(i = 0; i < tempCardData.length; i++)
						resultCardData.push(tempCardData[i].slice());
					tempCardData = analyResult.cbMissileCardData;
					for(i = 0; i < tempCardData.length; i++)
						resultCardData.push(tempCardData[i].slice());
					break;
				case CT_THREE_LINE_TAKE_ONE:
					if(analyResult.cbThreeCount > 0 && handleCardCount >= turnCarddData.length)
					{
						var threeTakeOneCount:uint = turnCarddData.length / 4;
						tempCardData = analyResult.cbSignedCardData;
						if(tempCardData.length > 0)
						{
							for(i = 0; i < threeTakeOneCount; i++)
								if(tempCardData[i])cardDataValues.push(tempCardData[i]);
						}
						if(cardDataValues.length < threeTakeOneCount)
						{
							tempCardData = analyResult.cbDoubleCardData;
							for(i = 0; i < threeTakeOneCount-cardDataValues.length; i++)
								if(tempCardData[i])cardDataValues.push(tempCardData[i][0]);
						}
						cardData.length = 0;
						SortCardList(turnCarddData, ST_COUNT);
						tempCardData = threeTakeOneCount>1 ? analyResult.cbAircraftCardData : analyResult.cbThreeCardData;
						for(i = 0; i < tempCardData.length; i++)
						{
							if(tempCardData[i].length/3 == threeTakeOneCount && GetCardLogicValue(tempCardData[i][tempCardData[i].length-1]) > GetCardLogicValue(turnCarddData[threeTakeOneCount*3-1]))
							{
								if(cardDataValues.length < threeTakeOneCount)
								{
									tempCardData2 = analyResult.cbThreeCardData;
									cardData.length = 0;
									for(j = 0; j < tempCardData2.length; j++)
									{
										for(k = 0; k < tempCardData[i].length/3; k++)
										{
											isPush = GetCardLogicValue(tempCardData[i][k*3]) != GetCardLogicValue(tempCardData2[j][0]);
											if(!isPush) break;
										}
										if(isPush)
										{
											cardData.push(tempCardData2[j][0]);
											if(cardDataValues.length + cardData.length == threeTakeOneCount)
											{
												tempCardData1 = tempCardData[i].slice();
												for(k = 0; k < cardDataValues.length; k++)
													tempCardData1.push(cardDataValues[k]);
												for(k = 0; k < cardData.length; k++)
													tempCardData1.push(cardData[k]);
												resultCardData.push(tempCardData1);
												break;
											}
										}
									}
									if(cardDataValues.length+cardData.length < threeTakeOneCount)
									{
										tempCardData2 = analyResult.cbFourCardData;
										for(j = 0; j < tempCardData2.length; j++)
										{
											for(k = 0; k < tempCardData[i].length/3; k++)
											{
												isPush = GetCardLogicValue(tempCardData[i][k*3]) != GetCardLogicValue(tempCardData2[j][0]);
												if(!isPush) break;
											}
											if(isPush)
											{
												cardData.push(tempCardData2[j][0]);
												if(cardDataValues.length + cardData.length == threeTakeOneCount)
												{
													tempCardData1 = tempCardData[i].slice();
													for(k = 0; k < cardDataValues.length; k++)
														tempCardData1.push(cardDataValues[k]);
													for(k = 0; k < cardData.length; k++)
														tempCardData1.push(cardData[k]);
													resultCardData.push(tempCardData1);
													break;
												}
											}
										}
									}
								}
								else
								{
									tempCardData1 = tempCardData[i].slice();
									for(j = 0; j < cardDataValues.length; j++)
										tempCardData1.push(cardDataValues[j]);
									resultCardData.push(tempCardData1);
								}
							}
						}
					}
					tempCardData = analyResult.cbFourCardData;
					for(i = 0; i < tempCardData.length; i++)
						resultCardData.push(tempCardData[i].slice());
					tempCardData = analyResult.cbMissileCardData;
					for(i = 0; i < tempCardData.length; i++)
						resultCardData.push(tempCardData[i].slice());
					break;
				case CT_THREE_LINE_TAKE_TWO:
					if(analyResult.cbThreeCount > 0 && handleCardCount >= turnCarddData.length)
					{
						var threeTakeTwoCount:uint = turnCarddData.length / 5;
						if(cardDataValues.length < threeTakeTwoCount*2)
						{
							tempCardData = analyResult.cbDoubleCardData;
							for(i = 0; i < threeTakeTwoCount; i++)
								if(tempCardData[i])cardDataValues.push(tempCardData[i][0], tempCardData[i][1]);
						}
						
						cardData.length = 0;
						SortCardList(turnCarddData, ST_COUNT);
						tempCardData = threeTakeTwoCount>1 ? analyResult.cbAircraftCardData : analyResult.cbThreeCardData;
						for(i = 0; i < tempCardData.length; i++)
						{
							if(tempCardData[i].length/3 == threeTakeTwoCount && GetCardLogicValue(tempCardData[i][tempCardData[i].length-1]) > GetCardLogicValue(turnCarddData[threeTakeTwoCount*3-1]))
							{
								if(cardDataValues.length < threeTakeTwoCount*2)
								{
									tempCardData2 = analyResult.cbThreeCardData;
									cardData.length = 0;
									for(j = 0; j < tempCardData2.length; j++)
									{
										for(k = 0; k < tempCardData[i].length/3; k++)
										{
											isPush = GetCardLogicValue(tempCardData[i][k*3]) != GetCardLogicValue(tempCardData2[j][0]);
											if(!isPush) break;
										}
										if(isPush)
										{
											cardData.push(tempCardData2[j][0], tempCardData2[j][1]);
											if(cardDataValues.length + cardData.length == threeTakeTwoCount*2)
											{
												tempCardData1 = tempCardData[i].slice();
												for(k = 0; k < cardDataValues.length; k++)
													tempCardData1.push(cardDataValues[k]);
												for(k = 0; k < cardData.length; k++)
													tempCardData1.push(cardData[k]);
												resultCardData.push(tempCardData1);
												break;
											}
										}
									}
									if(cardDataValues.length+cardData.length < threeTakeTwoCount*2)
									{
										tempCardData2 = analyResult.cbFourCardData;
										for(j = 0; j < tempCardData2.length; j++)
										{
											for(k = 0; k < tempCardData[i].length/3; k++)
											{
												isPush = GetCardLogicValue(tempCardData[i][k*3]) != GetCardLogicValue(tempCardData2[j][0]);
												if(!isPush) break;
											}
											if(isPush)
											{
												cardData.push(tempCardData2[j][0], tempCardData2[j][1]);
												if(cardDataValues.length + cardData.length == threeTakeTwoCount*2)
												{
													tempCardData1 = tempCardData[i].slice();
													for(k = 0; k < cardDataValues.length; k++)
														tempCardData1.push(cardDataValues[k]);
													for(k = 0; k < cardData.length; k++)
														tempCardData1.push(cardData[k]);
													resultCardData.push(tempCardData1);
													break;
												}
											}
										}
									}
								}
								else
								{
									tempCardData1 = tempCardData[i].slice();
									for(j = 0; j < cardDataValues.length; j++)
										tempCardData1.push(cardDataValues[j]);
									resultCardData.push(tempCardData1);
								}
							}
						}
						
//						if(cardDataValues.length==threeTakeTwoCount*2)
//						{
//							SortCardList(turnCarddData, ST_COUNT);
//							tempCardData = threeTakeTwoCount>1 ? analyResult.cbAircraftCardData : analyResult.cbThreeCardData;
//							for(i = 0; i < tempCardData.length; i++)
//							{
//								if(tempCardData[i].length/3 == threeTakeTwoCount && GetCardLogicValue(tempCardData[i][0]) > GetCardLogicValue(turnCarddData[0]))
//								{
//									tempCardData1 = tempCardData[i].slice();
//									for(j = 0; j < cardDataValues.length; j++)
//										tempCardData1.push(cardDataValues[j]);
//									resultCardData.push(tempCardData1);
//								}
//							}
//						}
					}
					tempCardData = analyResult.cbFourCardData;
					for(i = 0; i < tempCardData.length; i++)
						resultCardData.push(tempCardData[i].slice());
					tempCardData = analyResult.cbMissileCardData;
					for(i = 0; i < tempCardData.length; i++)
						resultCardData.push(tempCardData[i].slice());
					break;
				case CT_SINGLE_LINE:
					tempCardData = analyResult.cbSignedLineCardData;
					for(i = 0; i < tempCardData.length; i++)
					{
						if(tempCardData[i].length == turnCarddData.length && 
							GetCardLogicValue(tempCardData[i][0]) > GetCardLogicValue(turnCarddData[turnCarddData.length-1]))
							resultCardData.push(tempCardData[i].slice());
					}
					tempCardData = analyResult.cbFourCardData;
					for(i = 0; i < tempCardData.length; i++)
						resultCardData.push(tempCardData[i].slice());
					tempCardData = analyResult.cbMissileCardData;
					for(i = 0; i < tempCardData.length; i++)
						resultCardData.push(tempCardData[i].slice());
					break;
				case CT_DOUBLE_LINE:
					tempCardData = analyResult.cbDoubleLineCardData;
					for(i = 0; i < tempCardData.length; i++)
					{
						if(tempCardData[i].length == turnCarddData.length && 
							GetCardLogicValue(tempCardData[i][0]) > GetCardLogicValue(turnCarddData[turnCarddData.length-1]))
							resultCardData.push(tempCardData[i].slice());
					}
					tempCardData = analyResult.cbFourCardData;
					for(i = 0; i < tempCardData.length; i++)
						resultCardData.push(tempCardData[i].slice());
					tempCardData = analyResult.cbMissileCardData;
					for(i = 0; i < tempCardData.length; i++)
						resultCardData.push(tempCardData[i].slice());
					break;
				case CT_BOMB_CARD:
					tempCardData = analyResult.cbFourCardData;
					for(i = 0; i < tempCardData.length; i++)
					{
						if(GetCardLogicValue(tempCardData[i][0]) > GetCardLogicValue(turnCarddData[0]))
							resultCardData.push(tempCardData[i].slice());
					}
					tempCardData = analyResult.cbMissileCardData;
					for(i = 0; i < tempCardData.length; i++)
						resultCardData.push(tempCardData[i].slice());
					break;
				case CT_FOUR_LINE_TAKE_ONE:
					if(analyResult.cbFourCount > 0)
					{
						cardDataValues = [];
						tempCardData = analyResult.cbSignedCardData;
						for(i = 0; i < tempCardData.length; i++)
						{
							if(i > 2) break;
							cardDataValues.push(tempCardData[i]);
						}
						if(cardDataValues.length < 2)
						{
							tempCardData = analyResult.cbDoubleCardData;
							for(i = 0; i < tempCardData.length; i++)
							{
								if(i > 2) break;
								cardDataValues.push(tempCardData[i][0]);
							}
						}
						if(cardDataValues.length < 2)
						{
							tempCardData = analyResult.cbThreeCardData;
							for(i = 0; i < tempCardData.length; i++)
							{
								if(i > 2) break;
								cardDataValues.push(tempCardData[i][0]);
							}
						}
						if(cardDataValues.length >= 2)
						{
							SortCardList(turnCarddData, ST_COUNT);
							tempCardData = analyResult.cbFourCardData;
							for(i = 0; i < tempCardData.length; i++)
							{
								if(GetCardLogicValue(tempCardData[i][0]) > GetCardLogicValue(turnCarddData[0]))
								{
									tempCardData1 = tempCardData[i].slice();
									for(j = 0; j < cardDataValues.length; j++)
										tempCardData1.push(cardDataValues[j]);
									resultCardData.push(tempCardData1);
								}
							}
						}
					}
					tempCardData = analyResult.cbFourCardData;
					for(i = 0; i < tempCardData.length; i++)
						resultCardData.push(tempCardData[i].slice());
					tempCardData = analyResult.cbMissileCardData;
					for(i = 0; i < tempCardData.length; i++)
						resultCardData.push(tempCardData[i].slice());
					break;
				case CT_FOUR_LINE_TAKE_TWO:
					if(analyResult.cbFourCount > 0)
					{
						cardDataValues = [];
						if(cardDataValues.length < 4)
						{
							tempCardData = analyResult.cbDoubleCardData;
							for(i = 0; i < tempCardData.length; i++)
							{
								if(i > 2) break;
								cardDataValues.push(tempCardData[i][0], tempCardData[i][1]);
							}
						}
						if(cardDataValues.length < 4)
						{
							tempCardData = analyResult.cbThreeCardData;
							for(i = 0; i < tempCardData.length; i++)
							{
								if(i > 2) break;
								cardDataValues.push(tempCardData[i][0], tempCardData[i][1]);
							}
						}
						if(cardDataValues.length >= 4)
						{
							SortCardList(turnCarddData, ST_COUNT);
							tempCardData = analyResult.cbFourCardData;
							for(i = 0; i < tempCardData.length; i++)
							{
								if(GetCardLogicValue(tempCardData[i][0]) > GetCardLogicValue(turnCarddData[0]))
								{
									tempCardData1 = tempCardData[i].slice();
									for(j = 0; j < cardDataValues.length; j++)
										tempCardData1.push(cardDataValues[j]);
									resultCardData.push(tempCardData1);
								}
							}
						}
					}
					tempCardData = analyResult.cbFourCardData;
					for(i = 0; i < tempCardData.length; i++)
						resultCardData.push(tempCardData[i].slice());
					tempCardData = analyResult.cbMissileCardData;
					for(i = 0; i < tempCardData.length; i++)
						resultCardData.push(tempCardData[i].slice());
					break;
				case CT_THREE_LINE:
					tempCardData = analyResult.cbAircraftCardData;
					for(i = 0; i < tempCardData.length; i++)
					{
						if(tempCardData[i].length == turnCarddData.length && 
							GetCardLogicValue(tempCardData[i][0]) > GetCardLogicValue(turnCarddData[turnCarddData.length-1]))
							resultCardData[i] = tempCardData[i].slice();
					}
					tempCardData = analyResult.cbFourCardData;
					for(i = 0; i < tempCardData.length; i++)
						resultCardData.push(tempCardData[i].slice());
					tempCardData = analyResult.cbMissileCardData;
					for(i = 0; i < tempCardData.length; i++)
						resultCardData.push(tempCardData[i].slice());
					break;
			}
			
			
//			public static const CT_ERROR:uint					=	0									//错误类型
//			public static const CT_SINGLE:uint					=1									//单牌类型
//			public static const CT_DOUBLE:uint					=2									//对牌类型
//			public static const CT_THREE:uint					=	3									//三条类型
//			public static const CT_SINGLE_LINE:uint			=	4									//单连类型
//			public static const CT_DOUBLE_LINE:uint			=	5									//对连类型
//			public static const CT_THREE_LINE:uint				=6									//三连类型
//			public static const CT_THREE_LINE_TAKE_ONE:uint	=	7									//三带一单
//			public static const CT_THREE_LINE_TAKE_TWO:uint	=	8									//三带一对
//			public static const CT_FOUR_LINE_TAKE_ONE:uint		=9									//四带两单
//			public static const CT_FOUR_LINE_TAKE_TWO:uint		=10									//四带两对
//			public static const CT_BOMB_CARD:uint				=	11									//炸弹类型
//			public static const CT_MISSILE_CARD:uint			=	12									//火箭类型
		}
	}
}